package xq.gwt.ui.test;

import java.util.ArrayList;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.ui.TextBox;

import xq.gwt.mvc.view.TextPropertyView;
import xq.gwt.mvc.view.ViewChangedListener;

public class TextPropertyViewMock implements TextPropertyView{	
	private TextBox txtBox;
    ArrayList<ViewChangedListener> listeners = new ArrayList<ViewChangedListener>();

    public TextPropertyViewMock(TextBox textBox){
        txtBox = textBox;
        txtBox.addValueChangeHandler(new ValueChangeHandler<String>() {
			
			@Override
			public void onValueChange(ValueChangeEvent<String> event) {
				notifyViewChangedListeners();
				
			}
		});
    }

    public void setText(String text) {
        txtBox.setValue(text);        
        
    }

    public String getText() {
       return txtBox.getText();
    }

    public void setError(String errorMessage) {

    }

    public void clearError() {

    }

    public void setEnabled(boolean enabled) {
        
    }

    public void setRequired(boolean required) {

    }

    public void addViewChangedListener(ViewChangedListener listener) {
        listeners.add(listener);
    }

    public void removeViewChangedListener(ViewChangedListener listener) {
        listeners.remove(listener);
    }

    protected void notifyViewChangedListeners(){
        for (int i = 0; i < listeners.size(); i++) {
            listeners.get(i).viewChanged();

        }
    }

	@Override
	public void setName(String name) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setTitle(String title) {
		// TODO Auto-generated method stub
		
	}
}
